<script setup>

</script>

<template>
  <el-card>
    <!-- 查询条件开始 -->
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="98px">
      <el-form-item label="挂号项目名称" prop="regItemName">
        <el-input
            v-model="queryParams.data.regItemName"
            placeholder="请输入挂号项目名称"
            clearable
            size="small"
            style="width:240px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
            v-model="queryParams.data.status"
            placeholder="可用状态"
            clearable
            size="small"
            style="width:240px"
        >
          <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 查询条件结束 -->

    <!-- 表格工具按钮开始 -->
    <el-row :gutter="10" style="margin-bottom: 8px;">
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">
          批量删除
        </el-button>
      </el-col>
    </el-row>
    <!-- 表格工具按钮结束 -->

    <!-- 数据表格开始 -->
    <el-table border :data="registeredItemTableList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="挂号项目ID" align="center" prop="regItemId"/>
      <el-table-column label="挂号项目名称" align="center" prop="regItemName"/>
      <el-table-column label="挂号项目费用" align="center" prop="regItemFee"/>
      <el-table-column label="状态" prop="statusName" align="center"/>
      <el-table-column label="创建时间" align="center" prop="createTime"/>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-edit" size="mini" @click="handleUpdate(scope.row)">修改
          </el-button>
          <el-button type="text" icon="el-icon-delete" size="mini" @click="handleDelete(scope.row)" :disabled="single">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 数据表格结束 -->
    <!-- 分页控件开始 -->
    <el-pagination
        v-show="total>0"
        :current-page="queryParams.pageNo"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
    <!-- 分页控件结束 -->

    <!-- 添加修改弹出层开始 -->
    <el-dialog
        :title="title"
        :visible.sync="open"
        width="600px"
        center
        append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="挂号项目名称" prop="regItemName">
          <el-input v-model="form.regItemName" placeholder="请输入挂号项目名称" clearable size="small"/>
        </el-form-item>
        <el-form-item label="挂号项目金额" prop="regItemFee">
          <el-input-number v-model="form.regItemFee"  clearable size="small" :min="0"/>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
                :value="dict.dictValue"
            >{{ dict.dictLabel }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </span>
    </el-dialog>
    <!-- 添加修改弹出层结束 -->
  </el-card>
</template>

<script>
export default {
  data() {
    return {
      // 查询条件
      queryParams: {
        data: {},
        // regItemName: undefined,
        // status: undefined,
        pageNo: 1,
        pageSize: 5
      },

      // 挂号项目表格数据
      registeredItemTableList: [],
      // 总条数
      total: 0,
      // 添加或修改弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 挂号项目状态字典
      statusOptions: [],
      //非单个禁用
      single: false,
      //非多个禁用
      multiple: true,
      //批量删除的id集合
      regItemIds: [],
      // 添加或修改弹出层表单数据
      form: {
        regItemId: undefined,
        regItemName: undefined,
        regItemFee: undefined,
        status: undefined
      },
      // 表单验证规则
      rules: {
        regItemName: [
          {
            required: true,
            message: "请输入挂号项目名称",
            trigger: "blur"
          }
        ],
        regItemFee: [
          {
            required: true,
            message: "请输入挂号项目金额",
          }
        ]
      }
    }
  },
  created() {
    this.formdata()
    this.loadStatus()
  },
  methods: {
    //获取挂号项目表格数据
    formdata() {
      this.$http.post("/registeredItem/queryByPage", this.queryParams).then(resp => {
        if (resp.data.code === 200) {
          this.registeredItemTableList = resp.data.data.list
          this.total = resp.data.data.total
        }
      })
    },
    //查询的提交
    handleQuery() {
      this.formdata()
    },
    //查询状态字典
    loadStatus() {
      this.$http.get("/registeredItem/getStatus").then(resp => {
        this.statusOptions = resp.data.data
      })
    },
    //格式化状态
    statusFormatter(row) {
      let dictLabel = "未知"
      this.statusOptions.filter(item => {
        if (item.dictValue === row.status) {
          dictLabel = item.dictLabel
        }
      })
      return dictLabel
    },
    //页码改变的方法
    handleSizeChange(pageSize) {
      this.queryParams.pageSize = pageSize
      this.formdata()
    },
    //页码改变的方法
    handleCurrentChange(pageNo) {
      this.queryParams.pageNo = pageNo
      this.formdata()
    },
    //重置
    resetQuery() {
      this.queryParams = {
        /*regItemName: undefined,
        status: undefined,*/
        data:{},
        pageNo: 1,
        pageSize: 5
      }
      this.formdata()
      // location.reload()//页面刷新
    },
    //添加
    handleAdd() {
      this.formReSet()
      this.open = true
      this.title = '---添加挂号项目---'
    },
    //批量删除或删除
    handleDelete(row) {
      let ids = row.regItemId || this.regItemIds
      this.$confirm("确认删除选中的项吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$http.delete("/registeredItem/deleteByIds/" + ids).then(resp => {
          if (resp.data.code === 200) {
            this.$message.success("删除成功");
            this.formdata();
          }
        }).catch(error =>{
          this.$message.warning("删除失败");
        })
      }).catch(error => {
        this.$message.warning("取消删除");
      })
    },
    //修改
    handleUpdate(row) {
      this.formReSet()
      if (row != null) {
        this.form = row
      }
      this.open = true
      this.title = '---修改挂号项目---'
    },
    //表单重置
    formReSet(){
      this.$nextTick(() => {
        this.$refs.form.resetFields();
      })
      this.form = {
        regItemName: undefined,
        regItemFee: undefined,
        status: undefined
      }
    },
    //添加或修改的提交
    handleSubmit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.$http.post("/registeredItem/insertAndUpdate", this.form).then(resp => {
            if (resp.data.code === 200) {
              this.$message.success(resp.data.msg)
              this.open = false
              this.formdata()
            } else {
              this.$message.error("操作失败")
            }
          })
        }
      })
    },
    //添加或修改的取消
    cancel() {
      this.open = false
      this.form = {
        regItemName: undefined,
        regItemFee: undefined,
        status: undefined
      }
    },
    // 表单选择触发的方法
    handleSelectionChange(selection){
      this.regItemIds = selection.map(item => item.regItemId)
      this.single = selection.length > 1
      this.multiple = !selection.length
    },
  }
}
</script>
<style scoped>

</style>